package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.UserDao;
import com.cn.wanxi.dao.impl.UserImpl;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.service.IUserService;

import java.util.List;

public class UserServiceImpl implements IUserService {
    /**
     * 1.首先根据用户名取判断当前用户是否存在
     * 2.如果存在，则告诉用户不能注册，请重新输入用户名
     * 3.如果不存在，则直接增加
     *
     * @param userModel
     * @return
     */
    public String aa="fds";
    @Override
    public String register(UserModel userModel) {
//        if()
        UserDao userDao = new UserImpl();
        boolean aa = userDao.findByUserName(userModel.getUsername()) != null;
        if (aa) {
            return "当前用户已经存在，请重新输入";
        } else {
            int result = userDao.register(userModel);
            if (result == 1) {
                return "注册成功";
            } else {
                return "注册失败";
            }
        }

    }

    /**
     * 根据用户名和密码进行登录
     *
     * @param userModel
     * @return
     */
    @Override
    public String login(UserModel userModel) {
        if (userModel.getCode().equals(userModel.getSessionCode())) {
            if (findByUserNameAndPassWord(userModel)) {
                return "登录成功";
            } else {
                return "用户名或者密码错误，登录失败";
            }
        } else {
            return "验证码不正确";
        }

    }

    @Override
    public List<UserModel> findAll(UserModel userModel) {
        UserDao userDao = new UserImpl();
        return userDao.findAll(userModel);
    }

    @Override
    public UserModel findById(Integer id) {
        UserDao userDao = new UserImpl();
        return userDao.findById(id);
    }

    @Override
    public int update(UserModel userModel) {
        UserDao userDao = new UserImpl();
        return userDao.update(userModel);
    }

    @Override
    public int del(Integer id) {
        UserDao userDao = new UserImpl();
        return userDao.del(id);
    }

    @Override
    public int getCount(UserModel userModel) {
        UserDao userDao = new UserImpl();
        return userDao.getCount(userModel);
    }

    private boolean findByUserName(String username) {
        UserDao userDao = new UserImpl();
        return userDao.findByUserName(username) != null;
    }

    private boolean findByUserNameAndPassWord(UserModel userModel) {
        UserDao userDao = new UserImpl();
        return userDao.findByUserNameAndPassWord(userModel) != null;
    }
}
